package com.redistest.demo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@SpringBootApplication
@RestController
public class DemoApplication {
	private final RedisTemplate<String,Object> redisTemplate;

	public DemoApplication(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	@Bean
	public Object testBean(PlatformTransactionManager platformTransactionManager){
		System.err.println(platformTransactionManager.getClass().getName());
		return new Object();
	}

	public static void main(String[] args) {
		SpringApplication springApplication = new SpringApplication(DemoApplication.class);
		springApplication.setBannerMode(Banner.Mode.OFF);
		springApplication.run(args);
	}

	@GetMapping("/test")
	@Transactional(rollbackFor = Exception.class)
	public void test(){
		User user = new User("lc",22,LocalDate.now(),new Date(),LocalDateTime.now());
		redisTemplate.opsForValue().set(user.name,user);
		int i = 1/0;
	}

	@Data
	@ToString
	@AllArgsConstructor
	public static class User{
		private String name;
		private Integer age;
		private LocalDate date;
		private Date dateD;
		private LocalDateTime time;
	}
}
